package com.lxmes.procontrol.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.lxmes.base.domain.JcIteminfo;
import com.lxmes.base.domain.JcIteminfoJs;
import com.lxmes.base.domain.JcWarehouseinfoKuwei;
import com.lxmes.base.mapper.JcIteminfoJsMapper;
import com.lxmes.base.mapper.JcIteminfoMapper;
import com.lxmes.base.mapper.JcWarehouseinfoKuweiMapper;
import com.lxmes.business.domain.CcChukuInfo;
import com.lxmes.business.domain.CcRukuInfo;
import com.lxmes.business.domain.CcStockInfo;
import com.lxmes.business.mapper.CcChukuInfoMapper;
import com.lxmes.business.mapper.CcRukuInfoMapper;
import com.lxmes.business.mapper.CcStockInfoMapper;
import com.lxmes.common.annotation.DataScope;
import com.lxmes.common.exception.ServiceException;
import com.lxmes.common.utils.DateUtils;
import com.lxmes.common.utils.NewNumber;
import com.lxmes.common.utils.SecurityUtils;
import com.lxmes.procontrol.domain.NcProOrderInfoBg;
import com.lxmes.procontrol.domain.NcProProcessControl;
import com.lxmes.procontrol.mapper.NcProOrderInfoBgMapper;
import com.lxmes.procontrol.mapper.NcProProcessControlMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.lxmes.procontrol.mapper.NcProOrderInfoMapper;
import com.lxmes.procontrol.domain.NcProOrderInfo;
import com.lxmes.procontrol.service.INcProOrderInfoService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 工单明细Service业务层处理
 * 
 * @author 张志煜
 * @date 2022-12-29
 */
@Service
public class NcProOrderInfoServiceImpl implements INcProOrderInfoService
{
    @Autowired
    private NcProOrderInfoMapper ncProOrderInfoMapper;
    @Autowired
    private NcProOrderInfoBgMapper bgMapper;
    @Autowired
    private NcProProcessControlMapper controlMapper;
    @Autowired
    private JcIteminfoJsMapper jsMapper;
    @Autowired
    private JcIteminfoMapper iteminfoMapper;
    @Autowired
    private CcStockInfoMapper ccStockInfoMapper;
    @Autowired
    private CcRukuInfoMapper rukuInfoMapper;
    @Autowired
    private JcWarehouseinfoKuweiMapper kuweiMapper;
    @Autowired
    private CcChukuInfoMapper ccChukuInfoMapper;


    /**
     * 查询工单明细
     *
     * @param id 工单明细主键
     * @return 工单明细
     */
    @Override
    public NcProOrderInfo selectNcProOrderInfoById(Long id)
    {
        return ncProOrderInfoMapper.selectNcProOrderInfoById(id);
    }

    @Override
    public NcProOrderInfo getBgOrderInfo(Long id) {
        NcProOrderInfo orderInfo = ncProOrderInfoMapper.selectNcProOrderInfoById(id);
        orderInfo.setYbgNum(orderInfo.getBgNum());
        orderInfo.setBgNum(orderInfo.getPgNum().subtract(orderInfo.getBgNum()));
        return orderInfo;
    }


    /*报工所需物料*/
    @Override
    public JSONObject getBgOrderinfoItem(Long id) {
        JSONObject object=new JSONObject();
        NcProOrderInfo orderInfo = ncProOrderInfoMapper.selectNcProOrderInfoById(id);
        JcIteminfoJs js = jsMapper.selectJcIteminfoJsByLineCodeAndGwCode(orderInfo.getLineCode(), orderInfo.getGwCode());
        JcIteminfo info=new JcIteminfo();
        //原材料、基础料不存在识别码
        List<JcIteminfo> list =new ArrayList<>();
        //转换前 领料物料
        JSONArray zhqArray=new JSONArray();
        info.setClassId(js.getJdzhqItemId());
        info.setItemId(orderInfo.getDyYclCode());
        //如果是基础料和原材料不存在物料标识
        if(!"J".equals(js.getJdzhqItemId()) && !"Y".equals(js.getJdzhqItemId())){
            info.setItemIdentifyCode(orderInfo.getItemIdentifyCode());
        }
        list = iteminfoMapper.selectJcIteminfoList(info);
        for (JcIteminfo iteminfo:list){
            JSONObject zhqObj=new JSONObject();
            zhqObj.put("value",iteminfo.getItemId());
            zhqObj.put("label",iteminfo.getItemName());
            zhqArray.add(zhqObj);
        }
        //转换后 报工物料
        JSONArray zhhArray=new JSONArray();
        info.setClassId(js.getJdzhhItemId());
        info.setItemId(orderInfo.getDyProCode());
        info.setItemIdentifyCode(orderInfo.getItemIdentifyCode());
        list = iteminfoMapper.selectJcIteminfoList(info);
        for (JcIteminfo iteminfo:list){
            JSONObject zhhObj=new JSONObject();
            zhhObj.put("value",iteminfo.getItemId());
            zhhObj.put("label",iteminfo.getItemName());
            zhhArray.add(zhhObj);
        }
        object.put("llwlData",zhqArray);
        object.put("bgwlData",zhhArray);
        return object;

    }

    /**
     * 查询工单明细列表
     *
     * @param ncProOrderInfo 工单明细
     * @return 工单明细
     */
    @Override
    @DataScope(deptAlias = "du", userAlias = "du")
    public List<NcProOrderInfo> selectNcProOrderInfoList(NcProOrderInfo ncProOrderInfo)
    {
        return ncProOrderInfoMapper.selectNcProOrderInfoList(ncProOrderInfo);
    }

    /**
     * 新增工单明细
     *
     * @param ncProOrderInfo 工单明细
     * @return 结果
     */
    @Override
    public int insertNcProOrderInfo(NcProOrderInfo ncProOrderInfo)
    {
        ncProOrderInfo.setCreateTime(DateUtils.getNowDate());
        return ncProOrderInfoMapper.insertNcProOrderInfo(ncProOrderInfo);
    }

    @Override
    @Transactional
    public int addBgOrderInfo(NcProOrderInfo ncProOrderInfo) {
        NcProOrderInfo orderInfo = ncProOrderInfoMapper.selectNcProOrderInfoById(ncProOrderInfo.getId());
        orderInfo.setDyYclCode(ncProOrderInfo.getDyYclCode());
        orderInfo.setDyProCode(ncProOrderInfo.getDyProCode());
        orderInfo.setBgNum(orderInfo.getBgNum().add(ncProOrderInfo.getBgNum()));
        orderInfo.setReturnNum(ncProOrderInfo.getReturnNum());
        ncProOrderInfoMapper.updateNcProOrderInfo(orderInfo);
        NcProOrderInfoBg orderInfoBg=new NcProOrderInfoBg();
        BeanUtils.copyProperties(orderInfo,orderInfoBg);
        orderInfoBg.setId(null);
        orderInfoBg.setBgNum(ncProOrderInfo.getBgNum());
        orderInfoBg.setCreateTime(new Date());
        orderInfoBg.setCreateBy(SecurityUtils.getUsername());
        orderInfoBg.setUpdateTime(null);
        orderInfoBg.setUpdateBy(null);
        orderInfoBg.setScDate(new Date());
        String danhao= "BG"+ NewNumber.createNewNumber(bgMapper.getNewCode(DateUtils.getDate()));
        orderInfoBg.setBgdanhao(danhao);
        bgMapper.insertNcProOrderInfoBg(orderInfoBg);
                JcIteminfo info=new JcIteminfo();
                JcIteminfoJs js = jsMapper.selectJcIteminfoJsByLineCodeAndGwCode(orderInfoBg.getLineCode(), orderInfoBg.getGwCode());
                //1.领料物料出库
                CcStockInfo ccStockInfo=new  CcStockInfo();
                if (!"Y".equals(js.getJdzhqItemId())&& !"J".equals(js.getJdzhqItemId())) {
                    ccStockInfo.setSaleOrderCode(orderInfoBg.getOrderNo());
                }
                ccStockInfo.setItemId(orderInfoBg.getDyYclCode());
                List<CcStockInfo> stockinfoList = ccStockInfoMapper.selectCcStockInfoList2(ccStockInfo);
                if (stockinfoList.size() > 0) {
                    //原材料先计算重量出库
                    BigDecimal ckNum = orderInfoBg.getBgNum();
                    if ("Y".equals(js.getJdzhqItemId())) {
                         ckNum = orderInfoBg.getBgNum().multiply(new BigDecimal(orderInfo.getXlzl()));
                         orderInfo.setPgNum(orderInfo.getPgNum().multiply(new BigDecimal(orderInfo.getXlzl())));
                    }
                    //可用数量总和
                    BigDecimal jsNum = ccStockInfoMapper.countStockInfoKyNum(ccStockInfo);
                    if (ckNum.compareTo(jsNum) == 1) {
                        throw new ServiceException(orderInfo.getDyYclCode()+"领料出库数量"+ckNum+"大于库存数量：" +jsNum+ "报工失败！！！");
                    }
                    String number = "CK-" + NewNumber.createNewNumber(ccChukuInfoMapper.getNewCode(DateUtils.getDate()));
                    for (CcStockInfo stockInfo : stockinfoList) {
                        //可用数量
                        BigDecimal kyNum = stockInfo.getKcNum().subtract(stockInfo.getLockNum());
                        CcChukuInfo chukuInfo = new CcChukuInfo();
                        //如果第一条库存够的话直接扣除
                        if (kyNum.compareTo(ckNum) == 1 || kyNum.compareTo(ckNum) == 0) {
                            chukuInfo.setCkNum(ckNum);
                            BigDecimal ky = kyNum.subtract(ckNum);
                            BigDecimal kc = kyNum.subtract(ckNum).add(stockInfo.getLockNum());
                            stockInfo.setKcNum(kc);
                            stockInfo.setKyNum(ky);
                            ccStockInfoMapper.updateCcStockInfo(stockInfo);
                            //同时生成出库单
                            BeanUtils.copyProperties(stockInfo, chukuInfo);
                            chukuInfo.setUnitPrice(stockInfo.getUnitPrice());
                            chukuInfo.setTotalPrice(stockInfo.getUnitPrice().multiply(ckNum));
                            chukuInfo.setDanhao(number);
                            chukuInfo.setCkState(1L);
                            chukuInfo.setChukuType(4L);
                            chukuInfo.setJhckNum(orderInfo.getPgNum());
                            chukuInfo.setPlanId(orderInfo.getDanhao());
                            chukuInfo.setCreateBy(SecurityUtils.getUsername());
                            chukuInfo.setCreateTime(new Date());
                            chukuInfo.setChukuDate(new Date());
                            chukuInfo.setOrderDanhao(orderInfo.getOrderNo());
                            chukuInfo.setContractCode(orderInfo.getContractNo());
                            ccChukuInfoMapper.insertCcChukuInfo(chukuInfo);
                            break;
                            //否则循环减去
                        } else {
                            //剩余出库数量
                            ckNum = ckNum.subtract(kyNum);
                            chukuInfo.setCkNum(kyNum);
                            //库存数量-可用数量=库存数量
                            BigDecimal kc = stockInfo.getKcNum().subtract(kyNum);
                            BigDecimal ky = stockInfo.getKcNum().subtract(kyNum).subtract(stockInfo.getLockNum());
                            stockInfo.setKcNum(kc);
                            stockInfo.setKyNum(ky);
                            ccStockInfoMapper.updateCcStockInfo(stockInfo);
                            //同事生成出库单
                            BeanUtils.copyProperties(stockInfo, chukuInfo);
                            chukuInfo.setDanhao(number);
                            chukuInfo.setCkState(1L);
                            chukuInfo.setChukuType(4L);
                            chukuInfo.setJhckNum(orderInfo.getPgNum());
                            chukuInfo.setPlanId(orderInfo.getDanhao());
                            chukuInfo.setUnitPrice(stockInfo.getUnitPrice());
                            chukuInfo.setTotalPrice(stockInfo.getUnitPrice().multiply(kyNum));
                            chukuInfo.setCreateBy(SecurityUtils.getUsername());
                            chukuInfo.setCreateTime(new Date());
                            chukuInfo.setChukuDate(new Date());
                            chukuInfo.setOrderDanhao(orderInfo.getOrderNo());
                            chukuInfo.setContractCode(orderInfo.getContractNo());
                            ccChukuInfoMapper.insertCcChukuInfo(chukuInfo);
                        }
                    }
                } else {
                    throw new ServiceException("未找到领料物料："+orderInfo.getDyYclCode() + "的库存！！！");
                }
        //2、报工物料入库
        info=iteminfoMapper.selectJcIteminfoByItemIdAndClassIdAndIdentifyCode(orderInfo.getDyProCode(),null,orderInfo.getItemIdentifyCode());
        if(info!=null) {
            CcRukuInfo ccRukuInfo = new CcRukuInfo();
            BeanUtils.copyProperties(info, ccRukuInfo);
            ccRukuInfo.setLot(DateUtils.dateTime());
            String number = "RK-" + NewNumber.createNewNumber(rukuInfoMapper.getNewCode(DateUtils.getDate()));
            ccRukuInfo.setDanhao(number);
            ccRukuInfo.setRukuType(3L);
            JcWarehouseinfoKuwei stock = kuweiMapper.selectJcWarehouseinfoKuweiByKuweiJmCode(orderInfo.getGwCode());
            if (stock == null) {
                throw new ServiceException("仓库不存在！！！");
            }
            ccRukuInfo.setFactoryName("泰康");
            ccRukuInfo.setStockId(stock.getStockCode());
            ccRukuInfo.setKuquCode(stock.getKuquCode());
            ccRukuInfo.setKuquName(stock.getKuquName());
            ccRukuInfo.setKuweiCode(stock.getKuweiJmCode());
            ccRukuInfo.setStockId(stock.getStockCode());
            ccRukuInfo.setStockName(stock.getStockName());
            ccRukuInfo.setRukuDate(new Date());
            ccRukuInfo.setCreateTime(DateUtils.getNowDate());
            ccRukuInfo.setCreateBy(SecurityUtils.getUsername());
            ccRukuInfo.setRukuNum(orderInfoBg.getBgNum());
            ccRukuInfo.setOrderDanhao(orderInfoBg.getOrderNo());
            ccRukuInfo.setPlanId(orderInfoBg.getBgdanhao());
            ccRukuInfo.setContractCode(orderInfoBg.getContractNo());
            ccRukuInfo.setQualityState("0");
            rukuInfoMapper.insertCcRukuInfo(ccRukuInfo);
        }else {
            throw new ServiceException("报工物料未找到物料信息：" + orderInfoBg.getDyYclCode());
        }
        return 1;
    }
    /*public int addBgOrderInfo(NcProOrderInfo ncProOrderInfo) {
        NcProOrderInfo orderInfo = ncProOrderInfoMapper.selectNcProOrderInfoById(ncProOrderInfo.getId());
        orderInfo.setDyYclCode(ncProOrderInfo.getDyYclCode());
        orderInfo.setDyProCode(ncProOrderInfo.getDyProCode());
        orderInfo.setBgNum(orderInfo.getBgNum().add(ncProOrderInfo.getBgNum()));
        orderInfo.setReturnNum(ncProOrderInfo.getReturnNum());
        ncProOrderInfoMapper.updateNcProOrderInfo(orderInfo);
        NcProOrderInfoBg orderInfoBg=new NcProOrderInfoBg();
        BeanUtils.copyProperties(orderInfo,orderInfoBg);
        orderInfoBg.setId(null);
        orderInfoBg.setBgNum(ncProOrderInfo.getBgNum());
        orderInfoBg.setCreateTime(new Date());
        orderInfoBg.setCreateBy(SecurityUtils.getUsername());
        String danhao= "BG"+ NewNumber.createNewNumber(bgMapper.getNewCode(DateUtils.getDate()));
        orderInfoBg.setBgdanhao(danhao);
        bgMapper.insertNcProOrderInfoBg(orderInfoBg);
        JcIteminfo info=new JcIteminfo();
        JcIteminfoJs js = jsMapper.selectJcIteminfoJsByLineCodeAndGwCode(orderInfoBg.getLineCode(), orderInfoBg.getGwCode());
        //1.原材料基础料直接出库
        if ("J".equals(js.getJdzhqItemId()) || "Y".equals(js.getJdzhqItemId())) {
            info=iteminfoMapper.selectJcIteminfoByItemIdAndClassIdAndIdentifyCode(orderInfo.getDyYclCode(),null,null);
            List<CcStockInfo> stockinfoList = ccStockInfoMapper.selectCcStockInfoListByItemId(info.getItemId());
            if (stockinfoList.size() > 0) {
                //先计算重量出库
                //最原始材料无需下料重量
                BigDecimal ckNum = orderInfo.getBgNum();
                //可用数量总和
                BigDecimal jsNum = new BigDecimal("0");
                String number = "CK-" + NewNumber.createNewNumber(ccChukuInfoMapper.getNewCode(DateUtils.getDate()));
                for (CcStockInfo stockInfo : stockinfoList) {
                    BigDecimal kyNum = stockInfo.getKcNum().subtract(stockInfo.getLockNum());
                    jsNum = jsNum.add(kyNum);
                    CcChukuInfo chukuInfo = new CcChukuInfo();
                    //如果第一条库存够的话直接扣除
                    if (kyNum.compareTo(ckNum) == 1) {
                        chukuInfo.setCkNum(orderInfo.getPgNum());
                        stockInfo.setKcNum(stockInfo.getKcNum().subtract(ckNum));
                        stockInfo.setKyNum(stockInfo.getKcNum().subtract(stockInfo.getLockNum()));
                        ccStockInfoMapper.updateCcStockInfo(stockInfo);
                        //同事生成出库单
                        BeanUtils.copyProperties(stockInfo, chukuInfo);
                        chukuInfo.setUnitPrice(stockInfo.getUnitPrice());
                        chukuInfo.setTotalPrice(stockInfo.getUnitPrice().multiply());
                        chukuInfo.setDanhao(number);
                        chukuInfo.setCkState(1L);
                        chukuInfo.setChukuType(4L);
                        chukuInfo.setJhckNum(orderInfo.getPgNum());
                        chukuInfo.setPlanId(orderInfo.getDanhao());
                        chukuInfo.setCreateBy(SecurityUtils.getUsername());
                        chukuInfo.setCreateTime(new Date());
                        chukuInfo.setChukuDate(new Date());
                        ccChukuInfoMapper.insertCcChukuInfo(chukuInfo);
                        break;
                        //否则循环减去
                    } else {
                        ckNum = ckNum.subtract(kyNum);
                        chukuInfo.setCkNum(stockInfo.getKcNum());
                        stockInfo.setKcNum(stockInfo.getKcNum().subtract(kyNum));
                        stockInfo.setKyNum(stockInfo.getKcNum().subtract(stockInfo.getLockNum()));
                        ccStockInfoMapper.updateCcStockInfo(stockInfo);
                        //同事生成出库单
                        BeanUtils.copyProperties(stockInfo, chukuInfo);
                        chukuInfo.setDanhao(number);
                        chukuInfo.setCkState(1L);
                        chukuInfo.setChukuType(4L);
                        chukuInfo.setJhckNum(orderInfo.getPgNum());
                        chukuInfo.setPlanId(orderInfo.getDanhao());
                        chukuInfo.setCreateBy(SecurityUtils.getUsername());
                        chukuInfo.setCreateTime(new Date());
                        chukuInfo.setChukuDate(new Date());
                        ccChukuInfoMapper.insertCcChukuInfo(chukuInfo);
                    }

                }
                if (ckNum.compareTo(jsNum) == 1) {
                    throw new ServiceException(orderInfo.getDyYclCode() + "库存不足报工失败！！！");
                }
            } else {
                throw new ServiceException(orderInfo.getDyYclCode() + "库存不足报工失败！！！");
            }
        } else {
            //否则检验是否出库
            CcChukuInfo chukuInfo = new CcChukuInfo();
            chukuInfo.setItemId(orderInfo.getDyYclCode());
            chukuInfo.setCkState(1L);
            chukuInfo.setOrderDanhao(orderInfo.getOrderNo());
            BigDecimal ckNum = ccChukuInfoMapper.countCcChukuInfoNum(chukuInfo);
            BigDecimal bgNum = bgMapper.countBgNum(orderInfoBg);
            if (ckNum.compareTo(new BigDecimal("0")) == 0) {
                throw new ServiceException(orderInfo.getDyYclCode() + "未出库！！！");
            } else if (bgNum.compareTo(ckNum) == 1) {
                throw new ServiceException("报工数量：" + bgNum + "超出出库数量：" + ckNum);
            }
        }
        //2、报工物料入库
        info=iteminfoMapper.selectJcIteminfoByItemIdAndClassIdAndIdentifyCode(orderInfo.getDyProCode(),null,orderInfo.getItemIdentifyCode());
        if(info!=null) {
            CcRukuInfo ccRukuInfo = new CcRukuInfo();
            BeanUtils.copyProperties(info, ccRukuInfo);
            ccRukuInfo.setLot(DateUtils.dateTime());
            String number = "RK-" + NewNumber.createNewNumber(rukuInfoMapper.getNewCode(DateUtils.getDate()));
            ccRukuInfo.setDanhao(number);
            ccRukuInfo.setRukuType(3L);
            JcWarehouseinfoKuwei stock = kuweiMapper.selectJcWarehouseinfoKuweiByKuweiJmCode(orderInfo.getGwCode());
            if (stock == null) {
                throw new ServiceException("仓库不存在！！！");
            }
            ccRukuInfo.setStockId(stock.getStockCode());
            ccRukuInfo.setKuquCode(stock.getKuweiCode());
            ccRukuInfo.setKuquName(stock.getKuquName());
            ccRukuInfo.setKuweiCode(stock.getKuweiJmCode());
            ccRukuInfo.setStockId(stock.getStockCode());
            ccRukuInfo.setStockName(stock.getStockName());
            ccRukuInfo.setRukuDate(new Date());
            ccRukuInfo.setCreateTime(DateUtils.getNowDate());
            ccRukuInfo.setCreateBy(SecurityUtils.getUsername());
            ccRukuInfo.setRukuNum(orderInfo.getBgNum());
            ccRukuInfo.setOrderDanhao(orderInfo.getOrderNo());
            ccRukuInfo.setPlanId(orderInfoBg.getBgdanhao());
            ccRukuInfo.setContractCode(orderInfo.getContractNo());
            ccRukuInfo.setQualityState("待检");
            rukuInfoMapper.insertCcRukuInfo(ccRukuInfo);
        }else {
            throw new ServiceException("报工物料未找到物料信息：" + orderInfo.getDyYclCode());
        }
        return 1;
    }*/

/*
    public int addBgOrderInfo2(NcProOrderInfo ncProOrderInfo) {

        NcProOrderInfo orderInfo = ncProOrderInfoMapper.selectNcProOrderInfoById(ncProOrderInfo.getId());
       */
/* NcProProcessControl conQuery=new NcProProcessControl();
        conQuery.setGwCode(orderInfo.getGwCode());
        conQuery.setPcCode(orderInfo.getPcCode());*//*

*/
/*
        NcProProcessControl con = controlMapper.selectNcProProcessControlByPcCode(conQuery);
*//*

      */
/*  if(orderInfo.getUserId().longValue()!=SecurityUtils.getUserId() || Long.valueOf(con.getPgGwFzr()).longValue() !=SecurityUtils.getUserId()){
            throw new ServiceException("您不是负责人或报工人无权操作！！！");
        }*//*

        orderInfo.setDyYclCode(ncProOrderInfo.getDyYclCode());
        orderInfo.setDyProCode(ncProOrderInfo.getDyProCode());
        orderInfo.setBgNum(orderInfo.getBgNum().add(ncProOrderInfo.getBgNum()));
        orderInfo.setReturnNum(ncProOrderInfo.getReturnNum());
        ncProOrderInfoMapper.updateNcProOrderInfo(orderInfo);
        NcProOrderInfoBg orderInfoBg=new NcProOrderInfoBg();
        BeanUtils.copyProperties(orderInfo,orderInfoBg);
        orderInfoBg.setId(null);
        orderInfoBg.setBgNum(ncProOrderInfo.getBgNum());
        orderInfoBg.setCreateTime(new Date());
        orderInfoBg.setCreateBy(SecurityUtils.getUsername());
        String danhao= "BG"+ NewNumber.createNewNumber(bgMapper.getNewCode(DateUtils.getDate()));
        orderInfoBg.setBgdanhao(danhao);
        bgMapper.insertNcProOrderInfoBg(orderInfoBg);
        JcIteminfoJs js=new JcIteminfoJs();
        js.setLineCode(orderInfo.getLineCode());
        js.setGwCode(orderInfo.getGwCode());
        //1.查询(条件：生产线和工位)物料号转换表，唯一 否则报错
        List<JcIteminfoJs> jsList = jsMapper.selectJcIteminfoJsList(js);
        if(jsList.size()==0){
            throw new ServiceException("物料号转换无数据！！！");
        }else if(jsList.size()>1){
            throw new ServiceException("物料号转换数据不能重复！！！");
        }
        js=jsList.get(0);
        //2.查询物料表转换后的对用物料 唯一 否则报错
        JcIteminfo info=new JcIteminfo();

        if("TKY000000001".equals(js.getJdzhqItemId())){
            info.setItemId(js.getJdzhhItemId());
        }else {
            info.setClassId(js.getJdzhhItemId());
            info.setItemIdentifyCode(orderInfo.getItemIdentifyCode());
        }
        List<JcIteminfo> list=iteminfoMapper.selectJcIteminfoList(info);
        if(list.size()==0){
            throw new ServiceException(orderInfo.getItemIdentifyCode()+"物料识别码未找到该物料信息！！！");
        }else if(list.size()>1){
            throw new ServiceException("物料数据不能重复！！！");
        }
        info=list.get(0);
        //一、原材料报工情况
        //3.转换前是原材料默认出库
        if("Y".equals(js.getJdzhqItemId())){
            List<CcStockInfo> stockinfoList = ccStockInfoMapper.selectCcStockInfoListByItemId(orderInfo.getDyYclCode());
            if(stockinfoList.size()>0){
                //先计算重量出库
                //下料重量*报工数量=原材料出库数量
                BigDecimal ckNum = orderInfo.getBgNum().multiply(new BigDecimal(orderInfo.getXlzl()));
                //可用数量总和
                BigDecimal jsNum=new BigDecimal("0");
                String number= "CK-"+ NewNumber.createNewNumber(ccChukuInfoMapper.getNewCode(DateUtils.getDate()));
                for (CcStockInfo stockInfo:stockinfoList){
                    BigDecimal kyNum=stockInfo.getKcNum().subtract(stockInfo.getLockNum());
                    jsNum=jsNum.add(kyNum);
                    CcChukuInfo chukuInfo=new CcChukuInfo();
                    //如果第一条库存够的话直接扣除
                    if(kyNum.compareTo(ckNum)==1){
                        stockInfo.setKcNum(stockInfo.getKcNum().subtract(ckNum));
                        stockInfo.setKyNum(stockInfo.getKcNum().subtract(stockInfo.getLockNum()));
                        ccStockInfoMapper.updateCcStockInfo(stockInfo);
                        //同事生成出库单
                        BeanUtils.copyProperties(stockInfo,chukuInfo);
                        chukuInfo.setDanhao(number);
                        chukuInfo.setCkNum(ckNum);
                        chukuInfo.setCkState(1L);
                        chukuInfo.setChukuType(4L);
                        chukuInfo.setPlanId(orderInfo.getDanhao());
                        chukuInfo.setChukuDate(new Date());
                        chukuInfo.setCreateBy(SecurityUtils.getUsername());
                        chukuInfo.setCreateTime(new Date());
                        ccChukuInfoMapper.insertCcChukuInfo(chukuInfo);
                        break;
                        //否则循环减去
                    }else {
                        ckNum=ckNum.subtract(kyNum);
                        stockInfo.setKcNum(stockInfo.getKcNum().subtract(kyNum));
                        stockInfo.setKyNum(stockInfo.getKcNum().subtract(stockInfo.getLockNum()));
                        ccStockInfoMapper.updateCcStockInfo(stockInfo);
                        //同事生成出库单
                        BeanUtils.copyProperties(stockInfo,chukuInfo);
                        chukuInfo.setDanhao(number);
                        chukuInfo.setCkNum(ckNum);
                        chukuInfo.setCkState(1L);
                        chukuInfo.setChukuType(4L);
                        chukuInfo.setJhckNum(orderInfo.getPgNum());
                        chukuInfo.setPlanId(orderInfo.getDanhao());
                        chukuInfo.setCreateBy(SecurityUtils.getUsername());
                        chukuInfo.setCreateTime(new Date());
                        chukuInfo.setChukuDate(new Date());
                        ccChukuInfoMapper.insertCcChukuInfo(chukuInfo);
                    }

                }
                if(ckNum.compareTo(jsNum)==1){
                    throw new ServiceException(orderInfo.getDyYclCode()+"库存不足报工失败！！！");
                }
            }else {
                throw new ServiceException(orderInfo.getDyYclCode()+"库存不足报工失败！！！");
            }
        }else if("J".equals(js.getJdzhqItemId())){
            List<CcStockInfo> stockinfoList = ccStockInfoMapper.selectCcStockInfoListByItemId(orderInfo.getDyYclCode());
            if(stockinfoList.size()>0){
                //先计算重量出库
                //最原始材料无需下料重量
                BigDecimal ckNum = orderInfo.getBgNum();
                //可用数量总和
                BigDecimal jsNum=new BigDecimal("0");
                String number= "CK-"+ NewNumber.createNewNumber(ccChukuInfoMapper.getNewCode(DateUtils.getDate()));
                for (CcStockInfo stockInfo:stockinfoList){
                    BigDecimal kyNum=stockInfo.getKcNum().subtract(stockInfo.getLockNum());
                    jsNum=jsNum.add(kyNum);
                    CcChukuInfo chukuInfo=new CcChukuInfo();
                    //如果第一条库存够的话直接扣除
                    if(kyNum.compareTo(ckNum)==1){
                        stockInfo.setKcNum(stockInfo.getKcNum().subtract(ckNum));
                        stockInfo.setKyNum(stockInfo.getKcNum().subtract(stockInfo.getLockNum()));
                        ccStockInfoMapper.updateCcStockInfo(stockInfo);
                        //同事生成出库单
                        BeanUtils.copyProperties(stockInfo,chukuInfo);
                        chukuInfo.setDanhao(number);
                        chukuInfo.setCkNum(ckNum);
                        chukuInfo.setCkState(1L);
                        chukuInfo.setChukuType(4L);
                        chukuInfo.setJhckNum(orderInfo.getPgNum());
                        chukuInfo.setPlanId(orderInfo.getDanhao());
                        chukuInfo.setCreateBy(SecurityUtils.getUsername());
                        chukuInfo.setCreateTime(new Date());
                        chukuInfo.setChukuDate(new Date());
                        ccChukuInfoMapper.insertCcChukuInfo(chukuInfo);
                        break;
                        //否则循环减去
                    }else {
                        ckNum=ckNum.subtract(kyNum);
                        stockInfo.setKcNum(stockInfo.getKcNum().subtract(kyNum));
                        stockInfo.setKyNum(stockInfo.getKcNum().subtract(stockInfo.getLockNum()));
                        ccStockInfoMapper.updateCcStockInfo(stockInfo);
                        //同事生成出库单
                        BeanUtils.copyProperties(stockInfo,chukuInfo);
                        chukuInfo.setDanhao(number);
                        chukuInfo.setCkNum(ckNum);
                        chukuInfo.setCkState(1L);
                        chukuInfo.setChukuType(4L);
                        chukuInfo.setJhckNum(orderInfo.getPgNum());
                        chukuInfo.setPlanId(orderInfo.getDanhao());
                        chukuInfo.setCreateBy(SecurityUtils.getUsername());
                        chukuInfo.setCreateTime(new Date());
                        chukuInfo.setChukuDate(new Date());
                        ccChukuInfoMapper.insertCcChukuInfo(chukuInfo);
                    }

                }
                if(ckNum.compareTo(jsNum)==1){
                    throw new ServiceException(orderInfo.getDyYclCode()+"库存不足报工失败！！！");
                }
            }else {
                throw new ServiceException(orderInfo.getDyYclCode()+"库存不足报工失败！！！");
            }
        } else {
            //否则检验是否出库
            CcChukuInfo chukuInfo=new CcChukuInfo();
            chukuInfo.setItemId(orderInfo.getDyYclCode());
            chukuInfo.setCkState(1L);
            chukuInfo.setOrderDanhao(orderInfo.getOrderNo());
            BigDecimal ckNum = ccChukuInfoMapper.countCcChukuInfoNum(chukuInfo);
            BigDecimal bgNum = bgMapper.countBgNum(orderInfoBg);
            if(ckNum.compareTo(new BigDecimal("0"))==0){
                throw new ServiceException(orderInfo.getDyYclCode()+"未出库！！！");
            } else if(bgNum.compareTo(ckNum)==1){
                throw new ServiceException("派工数量："+orderInfo.getBgNum()+"超出出库数量："+ckNum);
            }
        }
        //4、转换后生成物料入库
        CcRukuInfo ccRukuInfo=new CcRukuInfo();
        BeanUtils.copyProperties(info,ccRukuInfo);
        ccRukuInfo.setLot(DateUtils.dateTime());
        String number= "RK-"+ NewNumber.createNewNumber(rukuInfoMapper.getNewCode(DateUtils.getDate()));
        ccRukuInfo.setDanhao(number);
        ccRukuInfo.setRukuType(3L);
        JcWarehouseinfoKuwei stock = kuweiMapper.selectJcWarehouseinfoKuweiByKuweiJmCode(js.getGwCode());
        if(stock==null){
            throw new ServiceException("仓库不存在！！！");
        }
        ccRukuInfo.setStockId(stock.getStockCode());
        ccRukuInfo.setKuquCode(stock.getKuweiCode());
        ccRukuInfo.setKuquName(stock.getKuquName());
        ccRukuInfo.setKuweiCode(stock.getKuweiJmCode());
        ccRukuInfo.setStockId(stock.getStockCode());
        ccRukuInfo.setStockName(stock.getStockName());
        ccRukuInfo.setRukuDate(new Date());
        ccRukuInfo.setCreateTime(DateUtils.getNowDate());
        ccRukuInfo.setCreateBy(SecurityUtils.getUsername());
        ccRukuInfo.setRukuNum(orderInfo.getBgNum());
        ccRukuInfo.setOrderDanhao(orderInfo.getOrderNo());
        ccRukuInfo.setPlanId(orderInfoBg.getDanhao());
        ccRukuInfo.setContractCode(orderInfo.getContractNo());
        ccRukuInfo.setQualityState("待检");
        rukuInfoMapper.insertCcRukuInfo(ccRukuInfo);
        return 1;
    }
*/

    @Override
    @Transactional
    public int addJdAllOrderInfo(Long[] ids) {
        for (int i = 0; i <ids.length ; i++) {
            NcProOrderInfo orderInfo = ncProOrderInfoMapper.selectNcProOrderInfoById(ids[i]);
            if("0".equals(orderInfo.getIsJd())){
                NcProOrderInfo info=new NcProOrderInfo();
                info.setId(orderInfo.getId());
                info.setIsJd("1");
                ncProOrderInfoMapper.updateNcProOrderInfo(info);
            }else{
                throw new ServiceException("接单的数据中存在已接单的数据请检查！！");
            }
        }
        return 1;
    }

    /**
     * 修改工单明细
     * 
     * @param ncProOrderInfo 工单明细
     * @return 结果
     */
    @Override
    public int updateNcProOrderInfo(NcProOrderInfo ncProOrderInfo)
    {
        ncProOrderInfo.setUpdateTime(DateUtils.getNowDate());
        ncProOrderInfo.setUpdateBy(SecurityUtils.getUsername());
        return ncProOrderInfoMapper.updateNcProOrderInfo(ncProOrderInfo);
    }

    /**
     * 批量删除工单明细
     * 
     * @param ids 需要删除的工单明细主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteNcProOrderInfoByIds(Long[] ids)
    {
        for (int i = 0; i <ids.length ; i++) {
            NcProOrderInfo orderInfo = ncProOrderInfoMapper.selectNcProOrderInfoById(ids[i]);
            NcProProcessControl conQuery=new NcProProcessControl();
            conQuery.setGwCode(orderInfo.getGwCode());
            conQuery.setPcCode(orderInfo.getPcCode());
            NcProProcessControl con = controlMapper.selectNcProProcessControlByPcCode(conQuery);
            con.setPgNum(con.getPgNum().subtract(orderInfo.getPgNum()));
            controlMapper.updateNcProProcessControl(con);
        }
        return ncProOrderInfoMapper.deleteNcProOrderInfoByIds(ids);
    }

    /**
     * 删除工单明细信息
     * 
     * @param id 工单明细主键
     * @return 结果
     */
    @Override
    public int deleteNcProOrderInfoById(Long id)
    {
        return ncProOrderInfoMapper.deleteNcProOrderInfoById(id);
    }
}
